home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalButtonUI.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  149 lines

  1. /*
  2.  * @(#)MetalButtonUI.java    1.14 98/04/10
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import com.sun.java.swing.plaf.*;
  29.  
  30. /**
  31.  * MetalButtonUI implementation
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @version 1.14 04/10/98
  41.  * @author Tom Santos
  42.  */
  43. public class MetalButtonUI extends BasicButtonUI {
  44.  
  45.     protected Color getSelectColor()       { return UIManager.getColor("Button.pressed"); }
  46.     protected Color getDisabledTextColor() { return UIManager.getColor("Button.disabledText"); }
  47.     protected Color getFocusColor()        { return UIManager.getColor("Button.focus"); }
  48.  
  49.     private final static MetalButtonUI metalButtonUI = new MetalButtonUI(); 
  50.  
  51.  
  52.     public static ComponentUI createUI(JComponent c) {
  53.         return metalButtonUI;
  54.     }
  55.  
  56.     public void installUI(JComponent c) {
  57.         super.installUI(c);
  58.         c.setOpaque(true);
  59.     }
  60.  
  61.     protected void paintButtonPressed(Graphics g, AbstractButton b) {
  62.         if (b.isOpaque() ) {
  63.             Dimension size = b.getSize();
  64.         g.setColor( getSelectColor() );
  65.         g.fillRect( 0, 0, size.width, size.height );
  66.     }
  67.     }
  68.  
  69.     protected void paintFocus(Graphics g, AbstractButton b,
  70.                   Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
  71.  
  72.         Rectangle focusRect = new Rectangle();
  73.     String text = b.getText();
  74.     boolean isIcon = b.getIcon() != null;
  75.  
  76.         // If there is text
  77.         if ( text != null & !text.equals( "" ) ) {
  78.           if ( !isIcon ) {
  79.             focusRect.setBounds( textRect );
  80.         }
  81.         else {
  82.             focusRect.setBounds( iconRect.union( textRect ) );
  83.         }
  84.         }
  85.         // If there is an icon and no text
  86.         else if ( isIcon ) {
  87.           focusRect.setBounds( iconRect );
  88.         }
  89.  
  90.         g.setColor(getFocusColor());
  91.     g.drawRect((focusRect.x-1), (focusRect.y-1),
  92.           focusRect.width+1, focusRect.height+1);
  93.  
  94.  
  95.         /*
  96.         g.setColor(getFocusColor());
  97.     if (b instanceof JButton) {
  98.             if ( ((JButton)b).isDefaultButton() ) {
  99.             g.drawRect(3,3, size.width-7, size.height-7);    
  100.         }
  101.     }
  102.     g.drawRect(2,2, size.width-5, size.height-5);
  103.     */    
  104.     }
  105.  
  106.  
  107.     protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  108.     AbstractButton b = (AbstractButton) c;                 
  109.     ButtonModel model = b.getModel();
  110.     FontMetrics fm = g.getFontMetrics();
  111.  
  112.     /* Draw the Text */
  113.     if(model.isEnabled()) {
  114.         /*** paint the text normally */
  115.         g.setColor(b.getForeground());
  116.         BasicGraphicsUtils.drawString(g,text, model.getMnemonic(),
  117.                       textRect.x,
  118.                       textRect.y + fm.getAscent());
  119.     }
  120.     else {
  121.         /*** paint the text disabled ***/
  122.         g.setColor(UIManager.getColor("Button.disabledText"));
  123.         BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  124.                       textRect.x, textRect.y + fm.getAscent());
  125.  
  126.     }
  127.     }
  128.  
  129.     protected BasicButtonListener createListener(JComponent c) {
  130.     return new MetalButtonListener((AbstractButton) c);
  131.     }
  132.  
  133. }
  134.  
  135. class MetalButtonListener extends BasicButtonListener
  136. {
  137.  
  138.     public MetalButtonListener(AbstractButton b) {
  139.       super(b);  
  140.     }
  141.  
  142.     public void focusGained(FocusEvent e) { 
  143.         Component c = (Component)e.getSource();
  144.     c.repaint();
  145.     }
  146. }
  147.    
  148.  
  149.